<?php
class Shopping_cart extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('product_home_model');
        $this->load->model('shopping_cart_model');
    }
    public function index()
    {
        $data['list_cart'] = $this->session->userdata('cart');
        $total = 0;
        if(empty($data['list_cart'])) 
        {
            $data['empty_buy'] = 'None Product';
            $data['list_cart'] = array();
        }
        else {
            foreach ($data['list_cart'] as $pro)
            {
                $total += $pro['qty']*$pro['price'];
            }
        }
        $total_pro = count($data['list_cart']);
        $this->session->set_userdata('total_product', $total_pro);
        $this->session->set_userdata('total_money', $total);
        $this->layout_home('shopping_cart/index', $data);
    }
    public function buy($pro_id = '')
    {
        
        $pro_info = $this->product_home_model->get_info($pro_id);
        foreach ($pro_info as $pro)
        {
            $id = $pro['pro_id'];
            $name = $pro['pro_name'];
            if($pro['pro_sale'] != '') 
            {
                $price = $pro['pro_sale'];
            }
            else 
            {
                $price = $pro['pro_price'];
            }
        }
        
        $quatity = 1;
        
        $money = $quatity*$price;
        $data[] = array(
                "id"  => $id,
                "name" => $name,
                "price" => $price,
                "qty" => $quatity,
                "money" => $money
        );
        $data_cart = $this->session->userdata('cart');
        
        if(empty($data_cart))
        {
             $this->session->set_userdata('cart', $data);
        }
        else
        {
            $flag = 0;
            foreach ($data_cart as $key => $product)
            {
                
                    if($product['id'] == $pro_id) 
                    {
                        $flag = 1;
                        $data_cart[$key]['qty'] +=  1;
                        $data_cart[$key]['money'] = $data_cart[$key]['qty']*$data_cart[$key]['price'];
                        $this->session->set_userdata('cart', $data_cart);
                    }
            }
            
            if($flag == 0)
            {
                $data_mg = array_merge($data_cart, $data);
                $this->session->set_userdata('cart', $data_mg);
            }
        }
        redirect('default/shopping_cart/index#cate');
        
    }
    public function delete($pro_id = '')
    {
        $data_cart = $this->session->userdata('cart');
        foreach ($data_cart as $key => $pro)
        {
            if($pro['id'] == $pro_id)
            {
                unset($data_cart[$key]);
                $this->session->set_userdata('cart', $data_cart);
            }
        }
        redirect('default/shopping_cart/index#cate');
    }
    public function update()
    {
        if($this->input->post('update_sc')) 
        {
            $qty = $this->input->post('quatity');
            $data_cart = $this->session->userdata('cart');
            $i = 0;
            foreach ($data_cart as $key => $pro)
            {
                if(!is_numeric($qty[$i]) || $qty[$i] <= 0)
                {
                    $qty[$i] = 1;
                }
                $data_cart[$key]['qty'] = $qty[$i];
                $data_cart[$key]['money'] = $data_cart[$key]['qty']*$data_cart[$key]['price'];
                $i++;
            }
            $this->session->set_userdata('cart', $data_cart);
            redirect('default/shopping_cart/index#cate');
        }
    }

    public function clearn()
    {
        $this->session->unset_userdata('cart');
        $this->session->unset_userdata('total_money');
        $this->session->unset_userdata('total_product');
        redirect('default/shopping_cart/index#cate');
    }
    public function get_pro()
    {
        $name = $this->input->post('cus_name');
        $phone = $this->input->post('cus_phone');
        $address = $this->input->post('cus_address');
        $note = $this->input->post('cus_note');
        $date = getdate();
        $datenow = $date['year'] . "-" . $date['mon'] . "-" . $date['mday'];
        $data = array(
            'cus_name' => $name,
            'cus_phone' => $phone,
            'cus_address' => $address,
            'cus_note' => $note,
            'order_date' => $datenow
        );
        $date_check = $this->check_form->check_sql($data);
        return $date_check;
    }
    public function checkout()
    {
        $data_pro = $this->session->userdata('cart');
        if(count($data_pro) > 1)
        {
        $this->form_validation->set_rules('cus_name', 'Custom name', 'required');
        $this->form_validation->set_rules('cus_phone', 'Custom phone number', 'required');
        $this->form_validation->set_rules('cus_address', 'Custom address', 'required');
        $this->form_validation->set_message('required', '%s  is not empty');
        $this->form_validation->set_error_delimiters('<i style="color:red">','</i>');
        if ($this->form_validation->run() === FALSE)
        {
            $this->layout_home('default/shopping_cart/checkout');
        }
        else
        {
            $data = $this->get_pro();
            $this->shopping_cart_model->action_tab('tbl_order', $data, '');
            $re_in_max = $this->shopping_cart_model->get_id_max();
            $id_max =  $re_in_max[0]['id_max'];
            $i = 0;
            foreach ($data_pro as $pro)
            {
                $data_detail[$i] = array(
                    "order_id" => $id_max,
                    "pro_id"   => $pro['id'],
                    "quatity"  => $pro['qty']
                );
                $this->shopping_cart_model->action_tab('tbl_order_detail', $data_detail[$i], '');
                $i++;
            }
            $this->session->unset_userdata('cart');
            $this->session->unset_userdata('total_money');
            $this->session->unset_userdata('total_product');
            $datas['suss'] = "Buy successfully";
            $datas['list_cart'] = array();
            $this->layout_home('default/shopping_cart/index',$datas);
        }
    }
    else
    {
        redirect('default/shopping_cart/index');
    }
    }
}

